package com.meme.editor.model.sprite2d;

import java.util.ListIterator;

import org.apache.log4j.Logger;

import com.meme.editor.EntityObject;
import com.meme.editor.EntityRefLink;
import com.meme.editor.PropertyValueModification;
import com.meme.editor.PropertyValueModificationList;

import com.meme.editor.property.PropertyFactory;
import com.meme.editor.property.Property;
import com.meme.editor.property.value.EntityLinkPropertyValue;



public class PureModuleGroup extends ModuleGroup
{
	static Logger logger = Logger.getLogger (PureModuleGroup.class);
	
	final static String GroupName_ImageModuleGroup = "Module Image";	
	final static String PropertyName_ModuleImage = "Module Image"; 
	
	final static String NodePath_ModuleImagesList = "@/" + Sprite2dFile.EntityName_ModuleImageSet + "/$_children"; 
	
	//ModuleImage mImage;
	//EntityRefLink mRefedImage;
	Property mMoudleImageProperty;
	
	public PureModuleGroup ()
	{
		this (null, null);
	}
	
	public PureModuleGroup (String name)
	{
		this (name, null);
	}
	
	public PureModuleGroup (String name, ModuleImage img)
	{
		super (name);
		
		// build property manager
		//createProperty(PropertyGroupName_ImageModuleGroup, PropertyName_ModuleImage, new StringPropertyValue (PropertyDefaultValue_ModuleImage));
		createPublicProperty (GroupName_ImageModuleGroup, PropertyName_ModuleImage, PropertyFactory.ValueType_EntityRef )
			.setParameter (EntityLinkPropertyValue.Param_NodePath, NodePath_ModuleImagesList);

		
		//
		//mRefedImage = new EntityRefLink (this);
		//mRefedImage = (EntityRefLink) ( getPublicProperties().getPropertyInGroup(GroupName_ImageModuleGroup, PropertyName_ModuleImage).getValue() );
		mMoudleImageProperty = getPublicProperty (GroupName_ImageModuleGroup, PropertyName_ModuleImage);
		
		setModuleImage (img);		
	}
	
	@Override
	public String getDefaultName ()
	{
		return "Pure Modules";
	}
	
	@Override
	public boolean isValid ()
	{
		return getModuleImage () != null && getModuleImage ().isValid();
	}
	
	@Override
	protected boolean isValidChild (EntityObject entity)
	{
		return entity instanceof PureModule;
	}
	
	public PureModule createNewModule (int x, int y, int w, int h, int insertAt)
	{
		PureModule imgModule = new PureModule (x, y, w, h);
		
		insertChild (imgModule, insertAt);
		
		return imgModule;
	}
	
	public PureModule createNewModule (int x, int y, int w, int h)
	{
		return createNewModule (x, y, w, h, getChildrenCount());
	}
	
	public PureModule createNewModule (int insertAt)
	{
		return createNewModule (0, 0, 0, 0, insertAt);
	}
	
	public PureModule createNewModule ()
	{
		return createNewModule (0, 0, 0, 0, getChildrenCount());
	}
	
	@Override
	public EntityObject insertNewChild (int insertPos)
	{
		return createNewModule (insertPos);
	}
	
	
	@Override
	protected void _handleModifyingPropertyValues (PropertyValueModificationList modications)
	{
		//...
		ListIterator<PropertyValueModification> iter = modications.listIterator();
		while ( iter.hasNext() )
		{
			PropertyValueModification mod = iter.next();
			
			if (   mod.mGroupName.equals (GroupName_ImageModuleGroup)
					&& mod.mPropertyName.equals (PropertyName_ModuleImage) )
				{
					//Sprite2dFile sprite2dFile = Sprite2dFile.getHostSprite2dFile (this);
					//if ( sprite2dFile != null )
						setModuleImage ( (ModuleImage) mod.mNewValue );	
					
					iter.remove();
				}
		}
				
			
		super._handleModifyingPropertyValues (modications);
	}
	
	
	public void setModuleImage (ModuleImage img)
	{
		//todo: if img is not same as the old one, notify listeners
		//mImage = img;
		
		//mRefedImage.setRefedEntity(img);
			
		
		changePublicPropertyValue(GroupName_ImageModuleGroup, PropertyName_ModuleImage, img );
	}
	
	public ModuleImage getModuleImage ()
	{
		//return mImage;
		//return (ModuleImage) ( mRefedImage.getRefedEntity() );
		return (ModuleImage) mMoudleImageProperty.getValue();
	}
}
